package org.dant.ant.extension.types;

import java.util.*;

import org.apache.tools.ant.types.DataType;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.Reference;

public class HostSet extends DataType {
  protected Vector hosts = new Vector();

  public int getHighWaterMark() {
    int cumulation = 0;
    if (isReference()) {
      hosts = getRef().getHosts();
    }
    
    for (Iterator iter=hosts.iterator(); iter.hasNext();) {
      cumulation += ((Host)iter.next()).getHighWaterMark();
    }
    return cumulation;
  }

  public void setRefid(Reference r) throws BuildException {
    if (!hosts.isEmpty()) {
      throw tooManyAttributes();
    }
    super.setRefid(r);
  }

  public void addHost(Host host) {
    if (isReference()) {
      throw noChildrenAllowed();
    }
    hosts.addElement(host);
  }

  public List list() {
    if (isReference()) {
      hosts = getRef().getHosts();
    }
    
    List result = new ArrayList();
    
    for (int i = 0; i < hosts.size(); i++) {
      Host host = (Host) hosts.elementAt(i);
      result.add(host);
    }
    return result;
  }

  protected Vector getHosts() {
    return this.hosts;
  }

  protected  HostSet getRef() {
    Object o = ref.getReferencedObject(getProject());
    return (HostSet) o;
  }
}
